<?php

namespace app\work\controller;
use think\Db;
class Login extends Base {
    /**
     * 账号密码登录
     * @return bool
     * 参数：账号、密码
     * 返回值：0参数错误，1手机号码错误，2密码格式错误，3没有此用户，4密码错误，5成功
     */
    public  function userLogin(){
        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['pass'])){
            return json_encode(['status'=>0,'msg'=>'参数错误']);
        }
        $phone = $_POST['phone'];
        $pass = $_POST['pass'];
        if (empty($pass) || !isset($pass)) {
            return json_encode(['status' => 2, 'msg' => '请填写正确的密码']);
        } else {
            $username = Db::table('up_work_user')
                ->where('user_phone', $phone)
                ->whereOr('user_mynum', $phone)
                ->find();
            if (empty($username)) {
                return json_encode(['status' => 3, 'msg' => '未查到此用户']);
            } else {
                if ($username['user_password'] != md5($pass)) {
                    return json_encode(['status' => 4, 'msg' => '密码错误']);
                } else {
                    if ($username['user_status'] == 2) {
                        return json(['status' => 100, 'msg' => '账号冻结']);
                    } else {
                        //更新token
                        $token = md5(date('YmdHis') . rand(100000, 999999));
                        Db::table('up_work_user')
                            ->where('user_phone', $phone)
                            ->whereOr('user_mynum', $phone)
                            ->update(['user_token' => $token]);
//                        $rytoken = $this->rongyun($username['user_id'],$username['user_username'],$username['user_image']);
                        $username = Db::table('up_work_user')
                            ->where('user_phone', $phone)
                            ->whereOr('user_mynum', $phone)
                            ->find();
//                        $username['rytoken'] = $rytoken['token'];
                        return json_encode(['status' => 200, 'msg' => $username]);
                    }
                }
            }
        }
    }

    /**
     * 手机登录或注册时发送的验证码
     * @return bool
     * 参数：phone type(1是登录 2是注册 3是忘记密码，第三方登录和快捷登录传4)
     * 返回值：0参数错误，1手机格式错误，21登录无此号，22注册有此号，3验证码发送失败，4返回md5加密验证码
     */
    public  function getPhoneCode(){//echo 1;die;
        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['type'])){
            return json_encode(['status'=>0,'msg'=>'参数错误']);
        }
        $num = rand(100000,999999);
        $phone = $_POST['phone'];
        $type = $_POST['type'];
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
            return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        $username = Db::table('up_work_user')->where('user_phone',$phone)->find();
        if($type != 4){
            if(empty($username) && $type==1){
                return json_encode(['status'=>21,'msg'=>'没有此手机号码用户']);
            }
            if(empty($username) && $type==3){
                return json_encode(['status'=>21,'msg'=>'没有此手机号码用户']);
            }
            if(!empty($username) && $type==2){
                return json_encode(['status'=>22,'msg'=>'已经有此手机号码用户']);
            }
        }
        $sendres = $this->duaxin($phone,$num);
        if($sendres['code'] != '00000'){
            return json_encode(['status'=>3,'msg'=>'短信平台错误：'.$sendres['msg']]);
        }else {
            //=============================
            $isset = Db::table('up_work_phonecode')->where('phone', $phone)->find();
            if ($isset) {
                $res = Db::table('up_work_phonecode')->where('phone', $phone)->update(['code' => md5($num), 'time2' => time(), 'status' => 0]);
            } else {
                $res = Db::table('up_work_phonecode')->insert(['phone' => $phone, 'code' => md5($num), 'time2' => time()]);
            }
            if (!$res) {
                return json_encode(['status' => 31, 'msg' => '数据库操作失败']);
            }
            //===============================================
            return json_encode(['status' => 200, 'msg' => md5($num)]);
        }
    }
    /**
     * 手机号码登录
     * @return bool
     * 参数：phone，code
     * 返回：0参数错误，1手机格式错误，21验证码错误，22验证码超时，3无此用户，4成功
     */

    public  function phoneLogin(){
        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['code'])){
            return json_encode(['status'=>0,'msg'=>'参数错误']);
        }
        $phone = $_POST['phone'];
        $code = $_POST['code'];

        if(!preg_match("/^1[34578]\d{9}$/", $phone)){
            return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        //=============================================
        $res = Db::table('up_work_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if($code != $res['code']){
            return json_encode(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 301){
            return json_encode(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json_encode(['status'=>23,'msg'=>'验证码已使用']);
        }
        $username = Db::table('up_work_user')
            ->where('user_phone',$phone)->find();
        if(empty($username)){
            return json_encode(['status'=>3,'msg'=>'没有此手机号码用户']);
        }
        if($username['user_status'] == 2){
            return json(['status'=>100,'msg'=>'账号冻结']);
        }else{
            //更新token
            $token = md5(date('YmdHis') . rand(100000,999999));
            Db::table('up_work_user')->where('user_phone',$phone)
                ->update(['user_token'=>$token]);
//                        $rytoken = $this->rongyun($username['user_id'],$username['user_username'],$username['user_image']);
            $username = Db::table('up_work_user')->where('user_phone',$phone)->find();
            Db::table('up_work_phonecode')->where('phone',$phone)->update(['status'=>1]);
//                        $username['rytoken'] = $rytoken['token'];
            return json_encode(['status'=>200,'msg'=>$username]);
        }
//        return json_encode(['status'=>200,'msg'=>$username]);
    }

    /**
     * 注册下一步的时候
     * @return bool
     * 参数：phone,code,password
     * 返回：0参数错误，1手机格式错误，21验证码错误，22验证码超时，3无此用户，4有其他未填项，5插入登录表失败，200成功
     */
    public  function haveNumGoNext(){
        $num = $_POST['num'];
        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $idcard = $_POST['idcard'];
        $have = Db::table('up_work_user')->where('user_mynum',$num)->find();
        if($have || empty($num)){
            $username = Db::table('up_work_user')->where('user_phone',$phone)->find();
            $username2 = Db::table('up_work_user')->where('user_idcard',$idcard)->find();
            if(!empty($username)){
                return json_encode(['status'=>3,'msg'=>'已经有此手机号码用户']);
            }
            if(!empty($username2)){
                return json_encode(['status'=>3,'msg'=>'已经有此身份证号']);
            }
            //=============================================
            $res = Db::table('up_work_phonecode')->where('phone',$phone)->find();
            //=============================================================
            if($res){
                if($code != $res['code']){
                    return json_encode(['status'=>21,'msg'=>'验证码错误']);
                }
                $now = time();
                if($now - $res['time2'] >= 301){
                    return json_encode(['status'=>22,'msg'=>'验证码超时']);
                }
                if($res['status']==1){
                    return json_encode(['status'=>23,'msg'=>'验证码已使用']);
                }
                return json_encode(['status'=>200,'msg'=>'ok']);
            }else{
                return json_encode(['status'=>0,'msg'=>'请获取验证码']);
            }
        }else{
            return json_encode(['status'=>0,'msg'=>'无此业务编号，请确认']);
        }
    }
    public  function userReg(){
        header("Access-Control-Allow-Origin:*");
        $phone = $_POST['phone'];
        $pass = $_POST['password'];
        $num = $_POST['num'];
        $name = $_POST['name'];
        $idcard = $_POST['idcard'];
        //-----------------------------
        $username = Db::table('up_work_user')->where('user_phone',$phone)->find();
        $username2 = Db::table('up_work_user')->where('user_idcard',$idcard)->find();
        if(!empty($username)){
            return json_encode(['status'=>3,'msg'=>'已经有此手机号码用户']);
        }
        if(!empty($username2)){
            return json_encode(['status'=>3,'msg'=>'已经有此身份证号']);
        }
        //-----------------------------
        $job = $_POST['job'];
        $zheng = $_POST['zheng'];
        $fan = $_POST['fan'];
        $shou = $_POST['shou'];
        $mynum = substr(base_convert(md5(uniqid(md5(microtime(true)),true)), 16, 10), 0, 5);
        $token = md5(date('YmdHis').rand(100000,999999));
        $data = array();
        $data['user_id'] = $this->uuid();
        $data['user_password'] = md5($pass);
        $data['user_time'] = date('Y-m-d H:i:s');
        $data['user_date'] = date('Y-m-d');
        $data['user_token'] = $token;
        $data['user_phone'] = $phone;
        $data['user_name'] = $name;
        $data['user_mynum'] = $this->getMyNum($mynum);
        $data['user_leadnum'] = $num;
        $data['user_idcard'] = $idcard;
        $data['user_job'] = $job;
        $data['user_zheng'] = $zheng;
        $data['user_fan'] = $fan;
        $data['user_shou'] = $shou;
        $res = Db::table('up_work_user')->insert($data);
        if($res){
            $user = Db::table('up_work_user')
                ->where('user_id',$data['user_id'])
                ->find();
            Db::table('up_work_phonecode')->where('phone',$phone)->update(['status'=>1]);
//            //注册完成积分+10
//            Db::table('up_integral')
//                ->insert(['integral_status'=>1,'integral_user_id'=>$id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>10]);
//            Db::table('up_work_user')
//                ->where('user_id',$id)
//                ->setInc('user_integral',10);
//            $rytoken = $this->rongyun($user['user_id'],$user['user_username'],$user['user_image']);
//            $user['rytoken'] = $rytoken['token'];
            return json_encode(['status'=>200,'msg'=>$user]);
        }else{
            return json_encode(['status'=>5,'msg'=>'生成登录数据失败']);
        }
    }



    /**
     * 忘记密码
     * @return bool
     * 参数：phone,code，newpass
     * 返回：0参数错误，1手机格式错误，21验证码错误，22验证码超时，3无此用户，200成功
     */

    public  function forgetPass(){
        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['code'])){
            return json_encode(['status'=>0,'msg'=>'参数错误']);
        }
        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $new = $_POST['newpass'];
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
            return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        if($new == '' || $code == ''){
            return json_encode(['status'=>2,'msg'=>'有未填项']);
        }
        $username = Db::table('up_work_user')->where('user_phone',$phone)->find();
        if(empty($username)){
            return json_encode(['status'=>3,'msg'=>'没有此手机号码用户']);
        }
        //=============================================
        $res = Db::table('up_work_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if($code != $res['code']){
            return json_encode(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 301){
            return json_encode(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json_encode(['status'=>23,'msg'=>'验证码已使用']);
        }
        $token = md5(date('YmdHis').rand(100000,999999));
        //,'user_token'=>$token
        $res = Db::table('up_work_user')->where('user_phone',$phone)->update(['user_password'=>md5($new),'user_token'=>$token]);
        Db::table('up_work_phonecode')->where('phone',$phone)->update(['status'=>1]);
        if($res){
            $user = Db::table('up_work_user')->where('user_phone',$phone)->find();
            return json_encode(['status'=>200,'msg'=>$user]);
        }else{
            return json_encode(['status'=>3,'msg'=>'密码重置失败']);
        }
    }

    /**
     * 更换手机号——1：给旧手机发送验证码
     * 参数： phone，code
     * @return string
     */
    public function changephone_old_yzm()
    {
        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $username = Db::table('up_work_user')->where('user_phone',$phone)->find();
        if(empty($username)){
            return json_encode(['status'=>3,'msg'=>'没有此手机号码用户']);
        }
        //=============================================
        $res = Db::table('up_work_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if($code != $res['code']){
            return json_encode(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 301){
            return json_encode(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json_encode(['status'=>23,'msg'=>'验证码已使用']);
        }
        return json_encode(['status'=>200,'msg'=>'ok']);
    }
    /**
     * 更换手机号——2：提交新手机
     * 参数：phone，code
     * @return string
     */
    public function changephone_new_yzm()
    {
        $phone = trim($_POST['phone'],' ');
        $uid = $_POST['user_id'];
        $code = trim($_POST['code'],' ');
        if($phone == "" || $code == ""){
            return json_encode(['status'=>0,'msg'=>'手机号和验证码不能为空']);
        }
        $username = Db::table('up_work_user')->where('user_phone',$phone)->find();
        if(!empty($username)){
            return json_encode(['status'=>0,'msg'=>'已经有此手机号码用户']);
        }
        //=============================================================
        $res = Db::table('up_work_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if(empty($res)){
            return json_encode(['status'=>0,'msg'=>'不是同一个手机号']);
        }
        if($code != $res['code']){
            return json_encode(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 301){
            return json_encode(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json_encode(['status'=>23,'msg'=>'验证码已使用']);
        }
        $res2 = Db::table('up_work_user')->where('user_id',$uid)->update(['user_phone'=>$phone]);
        if($res2){
            $new = Db::table('up_work_user')->where('user_id',$uid)->find();
            return json_encode(['status'=>200,'msg'=>$new]);
        }else{
            return json_encode(['status'=>0,'msg'=>'更新失败']);
        }

    }

}